package com.wedding.app.service.impl;

import com.wedding.app.service.EmailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 邮件服务实现类
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Override
    public boolean sendCodeEmail(String email, String code, String username) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(fromEmail);
            message.setTo(email);
            message.setSubject("某某莫婚礼线上体验中心 - 注册验证码");
            message.setText("某某莫婚礼线上体验中心提醒您，当前注册账号为：" + username + "，验证码为：" + code);

            mailSender.send(message);
            System.out.println("邮件发送成功，收件人：" + email + "，验证码：" + code);
            return true;
        } catch (Exception e) {
            System.err.println("邮件发送失败：" + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}